package dproxy.server.net.backoff;

import java.util.Collection;

/**
 * Generates timeout times following some algorithm.
 */
public abstract class IntervalGenerator {

    /** Linear implementation. */
    public static final String LINEAR = "LINEAR";
    /** Exponential implementation. */
    public static final String EXPONENTIAL = "EXPONENTIAL";
    /** x^n implementation. */
    public static final String POWER_N = "POWER_N";

    /**
     * Protected constructor.
     */
    protected IntervalGenerator() {
    }

    /**
     * @return Implementation instance, based on  configuration file
     */
    public static IntervalGenerator newInstance(String algorithm) {
        IntervalGenerator implementation;
        if (LINEAR.equals(algorithm)) {
            implementation = new LinearIntervalGenerator();
        } else if (EXPONENTIAL.equals(algorithm)) {
            implementation = new ExponentialIntervalGenerator();
        } else if (POWER_N.equals(algorithm)) {
            implementation = new PowerNIntervalGenerator();
        } else {
            implementation = null;
        }
        
        return implementation;
    }
    
    /**
     * @param readTimeout Maximum timeout
     * @param readRetestTimes How many times a "re-check" should be considered before
     * timeout
     * @return Collection with times to wait. The somatory of all times equals timeout
     */
    public abstract Collection<Integer> getIntervals(int readTimeout, int readRetestTimes);
    
}
